package com.zpark.servlet_session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户登录servlet
 *      用户登录成功之后，将用户名绑定到session域对象当中
 */
@WebServlet("/login.do")
public class Login extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理请求参数乱码
        req.setCharacterEncoding("utf-8");

        // 获取用户提交的登录数据
        String username = req.getParameter("username");

        // 获取session对象
        HttpSession session = req.getSession();
        // 将用户名添加到session域对象中
        session.setAttribute("name", username);

        // 响应登录成功页面(采用重定向方式)
        resp.sendRedirect("/Servlet_04_war/login_success.html");
    }
}
